package xchange;

/**
 * Class represents the router i/f 
 * in other words : handles router interaction
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.net.Socket;
import java.util.*;

public class Router {

	public String ip = "";
	public final int port = 9004;
	private BufferedReader in;
	private PrintWriter out;

	// tell router to start (= take over) downloading list of files
	public void takeoverDownload(ArrayList<Fileinfo> downloadList) {
		String command = "DOWNLOAD ";
		String line = "";
		String response = "";

		System.out.println(ip + " " + port);

		try {
			for (Fileinfo value : downloadList) {
				
				Socket ps = new Socket(ip, port);

				in = new BufferedReader(new InputStreamReader(ps.getInputStream()));

				out = new PrintWriter(ps.getOutputStream(), true);
				
				// and update info file
				File infoFile = new File("xchange/INFO/" + value.name
						+ ".!info");
				if (!infoFile.exists()) {
					System.err.println("ERROR : file xchange/INFO/"
							+ value.name + ".!info not found");
					return;
				}
				RandomAccessFile rafInfo;
				try {
					rafInfo = new RandomAccessFile(infoFile, "rw");

					try {
						line = rafInfo.readLine();
						
						System.out.println(line);
						
						rafInfo.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}

				command += value.name + "," + value.size + ","
						+ value.nr_of_blocks + "," + value.ip + "," + line
						+ ";";
				
				out.println(command);
				
				do {
					response = in.readLine();
				} while (response == null);	
				
				command = "DOWNLOAD ";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if (Debug.DEBUG) {
			System.out.println("Received after DOWNLOAD command send :" + line);
		}
	}

	// tell router to stop downloading
	public ArrayList<String[]> stopDownload() {
		String command = "STOP_ROUTER";
		String response = "";
		
		ArrayList<String[]> fileInfos = new ArrayList<String[]>();
		
		try {
			Socket ps = new Socket(ip, port);

			in = new BufferedReader(new InputStreamReader(ps.getInputStream()));

			out = new PrintWriter(ps.getOutputStream(), true);

			out.println(command);

			while ( (response = in.readLine()) != null ) {
				
				if ( response.equals("DONE")) {
					break;
				} else {		
					String[] result = response.split(":");
					
					fileInfos.add(result);
				}
			}
			
			ps.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		if (Debug.DEBUG) {
			System.out.println("Received after STOP command send :" + response);
		}
		
		return fileInfos;
		
	}

	// push local info files to router
	public void pushRtInfoFiles() {
		// your code here ...
	}

	// get all info files from router and merge them with local info files
	public void getRtInfoFiles() {
		// your code here ...
	}

	// remove all info files from router
	public void removeRtInfoFiles() {
		String command = "REMOVE";
		String response = "";

		try {
			Socket ps = new Socket(ip, port);

			in = new BufferedReader(new InputStreamReader(ps.getInputStream()));

			out = new PrintWriter(ps.getOutputStream(), true);

			out.println(command);

			do {
				response = in.readLine();
			} while (response == null);

			ps.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		if (Debug.DEBUG) {
			System.out.println("Received after REMOVE command send :"
					+ response);
		}
	}
}
